﻿using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
using Newtonsoft.Json;

namespace Aierp.Utility.Mail
{
    public class SendClound
    {
        /// <summary>
        /// 发送忘记密码的邮件,并获取结果
        /// </summary>
        /// <param name="email"></param>
        /// <param name="passUrl"></param>
        /// <param name="userName"></param>
        /// <returns></returns>
        public async Task<dynamic> SendforGotPassEmail(string email,string passUrl, string userName)
        {
            var from = "services@btcup.cn";
            var fromName = "btcup.cn";
            var subject = "btcup.cn重置密码";
            var templateInvokeName = "forGotPassWord";
            var xsmtpapi = ConvertXsmtpapi(email,passUrl, userName);
            var result = await Send(from, fromName, subject, templateInvokeName, xsmtpapi);
            return result;
        }

        /// <summary>
        /// 发送注册邮件
        /// </summary>
        /// <param name="email"></param>
        /// <param name="passUrl"></param>
        /// <param name="userName"></param>
        /// <returns></returns>
        public async Task<dynamic> SendRegEmail(string email, string passUrl, string userName)
        {
            var from = "services@btcup.cn";
            var fromName = "btcup.cn";
            var subject = "已经收到您的注册申请!";
            var templateInvokeName = "regEmail";
            var xsmtpapi = ConvertXsmtpapi(email, passUrl, userName);
            var result = await Send(from, fromName, subject, templateInvokeName, xsmtpapi);
            return result;
        }

        /// <summary>
        /// 发送邮件，可发送任意指定模板的邮件
        /// </summary>
        /// <param name="from"></param>
        /// <param name="fromName"></param>
        /// <param name="subject"></param>
        /// <param name="templateInvokeName"></param>
        /// <param name="xsmtpapi"></param>
        /// <returns></returns>
        public async Task<dynamic> Send(string from, string fromName, string subject,string templateInvokeName, string xsmtpapi)
        {
            HttpClient _client = new HttpClient();
            var postUrl = "http://api.sendcloud.net/apiv2/mail/sendtemplate";
            //var xsmtpapi = Convert(userName, token, url);
            //使用FormUrlEncodedContent做HttpContent
            var content = new FormUrlEncodedContent(new Dictionary<string, string>()
            {
                {"apiUser", "zuodong"},
                {"apiKey", "J08C9gxNGhinSfxR"},
                {"from", from},
                {"fromName", fromName},
                {"subject", subject},
                {"templateInvokeName", templateInvokeName},
                {"xsmtpapi", xsmtpapi}

            });
            var response = await _client.PostAsync(postUrl, content);
            var result = await response.Content.ReadAsStringAsync();
            return result;
        }

        /// <summary>
        /// 设置Xsmtpapi参数
        /// </summary>
        /// <param name="email"></param>
        /// <param name="passUrl"></param>
        /// <param name="userName"></param>
        /// <returns></returns>
        public string ConvertXsmtpapi(string email,string passUrl, string userName)
        {
            var json = "{\"to\":[\"" +
                       email +
                       "\"],\"sub\":{\"%userName%\":[\"" +
                       userName +
                       "\"],\"%sendDate%\":[\"" +
                       System.DateTime.Now.ToString("yyyy-MM-dd") +
                       "\"],\"%passUrl%\":[\"" +
                       passUrl +
                       "\"]}}";

            return json;
        }
    }
}
